package com.samsung.svmc.moneymanager.data;

import java.util.ArrayList;
import com.samsung.svmc.moneymanager.object.TienTe;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbTienTe extends DbConnect {
	
	protected String tag = "SQLConnection";
	protected static DbTienTe instance;
	protected Context context;
	protected DbHelper _help;
	public static final String TABLE = "tbl_tiente";
	public static final String Id = "id";
	public static final String TenTienTe = "ten";
	public static final String KyHieu = "kihieu";
	public static final String Icon = "icon_id";
	public static final String DonViTinh = "donvitinh";
	
	public DbTienTe(Context c){
		context = c;
	}
	
	public static DbTienTe getInstance(Context c){
		if(instance == null)
			instance = new DbTienTe(c);
		return instance;
	}
	
	public DbTienTe open() throws SQLException{
		_help = new DbHelper(context);
		_Db = _help.getWritableDatabase();
		return this;
	}
	
	public void close(){
		_help.close();
	}
	
	public void insert(TienTe t){
		ContentValues cv = new ContentValues();
		cv.put(TenTienTe, t.get_TenTienTe());
		cv.put(KyHieu, t.get_KiHieu());
		cv.put(Icon, t.IconId);
		cv.put(DonViTinh, t.get_DonViTinh());
		_Db.insert(TABLE, null, cv);
	}
	
	public void update(TienTe t){
		ContentValues cv = new ContentValues();
		cv.put(TenTienTe, t.get_TenTienTe());
		cv.put(KyHieu, t.get_KiHieu());
		cv.put(Icon, t.IconId);
		cv.put(DonViTinh, t.get_DonViTinh());
		_Db.update(TABLE, cv,Id+"="+t.get_Id(), null);
	}
	
	public void delete(long index){
		_Db.delete(TABLE, Id+"="+index, null);
	}
	
	public TienTe getTien(long index){
		TienTe tmp = new TienTe();
		Cursor c = _Db.query(TABLE, null, Id+"=?", new String[]{index+""}, null, null, null);
		if(c == null)
			Log.d(tag, "table tien te has not data");
		int iId = c.getColumnIndex(Id);
		int iTen = c.getColumnIndex(TenTienTe);
		int iKyHieu = c.getColumnIndex(KyHieu);
		int iIcon = c.getColumnIndex(Icon);
		int iDonVT = c.getColumnIndex(DonViTinh);
		while(c.moveToNext()){
			tmp.set_Id(c.getInt(iId));
			tmp.set_DonViTinh(c.getInt(iDonVT));
			tmp.set_KiHieu(c.getString(iKyHieu));
			tmp.set_TenTienTe(c.getString(iTen));
			tmp.IconId = c.getInt(iIcon);
			break;
		}
		c.close();
		return tmp;
	}
	
	public ArrayList<TienTe> getData(){
		ArrayList<TienTe> lv = new ArrayList<TienTe>();
		Cursor c = _Db.query(TABLE, null, null, null, null, null, null);
		if(c == null)
			Log.d(tag, "table tien te has not data");
		int iId = c.getColumnIndex(Id);
		int iTen = c.getColumnIndex(TenTienTe);
		int iKyHieu = c.getColumnIndex(KyHieu);
		int iIcon = c.getColumnIndex(Icon);
		int iDonVT = c.getColumnIndex(DonViTinh);
		while(c.moveToNext()){
			TienTe tmp = new TienTe();
			tmp.set_Id(c.getInt(iId));
			tmp.set_DonViTinh(c.getInt(iDonVT));
			tmp.set_KiHieu(c.getString(iKyHieu));
			tmp.set_TenTienTe(c.getString(iTen));
			tmp.IconId = c.getInt(iIcon);	
			lv.add(tmp);
		}
		c.close();
		return lv;
	}
	
private static class DbHelper extends SQLiteOpenHelper {
		
		public DbHelper(Context c){
			super(c,DATABASE_NAME , null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String sql = "CREATE TABLE " + TABLE + " ("
					+ Id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
					+ TenTienTe + " TEXT, "					
					+ KyHieu + " TEXT, "
					+ Icon + " INTEGER, "
					+ DonViTinh + " INTEGER "
			+ ");";
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			String sql = " DROP TABLE IF EXISTS " + TABLE;
			db.execSQL(sql);
			onCreate(db);
		}
		
	}
}
